import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

class CreateHashMap extends Thread {
	
	private Peer parent;

	public CreateHashMap(Peer parent)
	{
		this.parent = parent;
	}
	
	public void run()
	{
		parent.hashvalues = new HashMap<String, String>();
		ArrayList<String> filepaths = getFilepathsFromFolder(parent.dir);
		Iterator<String> i = filepaths.iterator();
		while(i.hasNext())
		{
			String s = i.next();
			try {
				parent.hashvalues.put(s, QueryFactory.getHashValue(parent.algorithm, s));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	
	ArrayList<String> getFilepathsFromFolder (String folder) //retorna o path de todos os ficheiros dentro da devida pasta e subpasta
	{
		ArrayList<String> result = new ArrayList<>();
		String[] files = new File(folder).list();
		for(int i=0; i < files.length; i++)
		{
			if(new File(folder + "\\" + files[i]).isFile())
			{
				result.add(folder +  "\\" + files[i]);
				
			}
			else
				result.addAll( getFilepathsFromFolder(folder + "\\" + files[i]) );
		}
		return result;
	}
	
	
}